<?php

$GLOBALS['template_data']['to_date'] = date("d/m/Y");

if (!empty($_REQUEST['to_date'])) {
    $GLOBALS['template_data']['to_date'] = $_REQUEST['to_date'];
    
    $exploded_date = explode("/", $_REQUEST['to_date']);
    $dates = array();
    $dates[$_REQUEST['to_date']] = base_obj::convertDateTo($_REQUEST['to_date']);
    
    $to_date = mktime(12, 0, 0, $exploded_date[1], 1, $exploded_date[2]);
    
    for ($i=0;$i<6;$i++) {
        $date = mktime(12, 0, 0, date('m', $to_date)-$i, 0, date('y', $to_date));
        $dates[date("m/Y",$date)] = date('Y-m-d', $date);
    }
    
    $user_obj = new user_obj();
    $cash_obj = new cash_obj();
    
    $filter = array();
    $filter['levels'] = array(
        USER_TYPE_ADMIN, USER_TYPE_TECHNIC
    );
    $filter['active'] = 1;
    
    $users = $user_obj->getUsers("*", $filter, $GLOBALS['list_of_users_order']);
    $GLOBALS['template_data']['dates'] = $dates;
    $GLOBALS['template_data']['output'] = array();
    
    if (!error_obj::isError($users) && !empty($users)) {
        
        foreach ($users as $i => $user) {
            $data = array();
            $data['name'] = $user['name'];
            $data['surname'] = $user['surname'];
            $data['dates'] = array();
            foreach ($dates as $key => $d) {
                $data['dates'][$key] = $cash_obj->getCountToDate($d, $user['users_id']);
            }
            
            $GLOBALS['template_data']['output'][$user['users_id']] = $data;
        }
        
        $total_data = array();
        $total_data['name'] = 'Total';
        $total_data['surname'] = "";
        $total_data['dates'] = array();
        foreach ($dates as $key => $d) {
            $total_data['dates'][$key] = $cash_obj->getCountToDate($d);
        }
        $GLOBALS['template_data']['output'][-1] = $total_data;
        
    }
    
}

?>
